#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Weazel.Gezel.Simulator.Reference
{
  /// <summary>
  /// The SignalDisplay is used when a $display directive
  /// contains an expression which may reference simulation
  /// signals
  /// </summary>
  class SignalDisplay : AbstractDisplay
  {
    /// <summary>
    /// References the signal to display
    /// </summary>
    private readonly AbstractSignal signal;

    /// <summary>
    /// Initializes the SignalDisplay with
    /// the AbstractSignal to show
    /// </summary>
    /// <param name="signal"></param>
    public SignalDisplay(AbstractSignal signal) 
    {
      this.signal = signal;
    }

    /// <summary>
    /// The radix used to convert the
    /// signal value to a string litteral
    /// </summary>
    /// <param name="base"></param>
    public override void SetBase(int @base)
    {
      signal.SetBase(@base);
    }

    /// <summary>
    /// Ouput the value of the signal to the buffer using 
    /// the specified radix. If the signal is only used in
    /// the $display it may need to be evaluated before it
    /// is shown
    /// </summary>
    /// <param name="g">simulation cycle</param>
    /// <param name="buffer">output buffer</param>
    public override void Show(long g, StringBuilder buffer)
    {
      // has the signal been evaluated
      // in the current generation
      if (signal.IsGeneration(g))
      {
        // ok, just show
        signal.Show(buffer);
      }
      else
      {
        // need to evaluate first
        signal.Evaluate(null, g);
        signal.Show(buffer);
      }
    }
  }
}
